package com.ship.dispatch.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ship.common.core.base.Page;
import com.ship.common.core.web.domain.JsonResult;
import com.ship.common.security.utils.SecurityUtils;
import com.ship.dispatch.bean.sys.SysLoginRecord;
import com.ship.dispatch.bean.sys.SysOperationRecord;
import com.ship.dispatch.service.ExportService;
import com.ship.dispatch.service.sys.SysLoginRecordService;
import com.ship.dispatch.service.sys.SysOperationRecordService;
import com.ship.system.api.RemoteUserService;
import com.ship.system.api.domain.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Field;
import java.util.LinkedList;
import java.util.List;

/**
 * @author yl
 * @title SysUserController
 * @description
 * @create 2023/12/20 0020
 */
@RestController
@RequestMapping("/sys")
@Api(value = "sysUser", tags = "系统用户控制器")
public class SysUserController {

    @Autowired
    private RemoteUserService remoteUserService;
    @Autowired
    SysLoginRecordService loginRecordService;

    @Autowired
    SysOperationRecordService operationRecordService;

    @Autowired
    private ExportService exportService;

    @GetMapping("/users")
    public JsonResult findUsers() {
        List<SysUser> sysUser = remoteUserService.getUserListInfo();
        return JsonResult.success(sysUser);
//        List<SysUser> userList = sysUserService.list(new LambdaQueryWrapper<SysUser>().eq(SysUser::getEnable, 1));
//        return JsonResult.success(userList);
    }

    /**
     * 获取登录日志列表
     *
     * @param loginRecord
     * @return
     */
    @ApiOperation(value = "获取登录日志分页列表")
    @PostMapping("/page")
    public JsonResult<Page<SysLoginRecord>> page(@RequestBody SysLoginRecord loginRecord) {
        Page<SysLoginRecord> userList = loginRecordService.page(loginRecord, new QueryWrapper<>());
        return JsonResult.success(userList);
    }


    /**
     * 登录日志列表导出
     *
     * @return
     */
    @ApiOperation(value = "登录日志列表导出")
    @PostMapping("/loginRecord/export")
    public JsonResult loginRecordExport(@RequestBody SysLoginRecord SysLoginRecord) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        if(SysLoginRecord.getIds() != null && SysLoginRecord.getIds().size() > 0){
            queryWrapper.in("id",SysLoginRecord.getIds());
        }
        List<SysLoginRecord> exportList = loginRecordService.pageExportList(SysLoginRecord, queryWrapper,new LinkedList<>());
        for (SysLoginRecord loginRecord : exportList) {
            loginRecord.setLoginTypeStr(loginRecord.getLoginTypeStr());
        }

        Field[] fields = SysLoginRecord.class.getDeclaredFields();
        String url = exportService.pageExport(exportList,fields,exportService.listStrByModuleAndUser
                ("sys_login_record", SecurityUtils.getUserId()), "登录日志信息");
        return JsonResult.success(url);
    }

    /**
     * 获取操作日志分页列表
     *
     * @param operationRecord
     * @return
     */
    @ApiOperation(value = "获取操作日志分页列表")
    @PostMapping("/operationRecordPage")
    public JsonResult<Page<SysOperationRecord>> page(@RequestBody SysOperationRecord operationRecord) {
        Page<SysOperationRecord> operationRecords = operationRecordService.page(operationRecord, new QueryWrapper<>());
        return JsonResult.success(operationRecords);
    }

    /**
     * 操作日志列表导出
     *
     * @return
     */
    @ApiOperation(value = "操作日志列表导出")
    @PostMapping("/operationRecord/export")
    public JsonResult operationRecordExport(@RequestBody SysOperationRecord operationRecord) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        if(operationRecord.getIds() != null && operationRecord.getIds().size() > 0){
            queryWrapper.in("id",operationRecord.getIds());
        }
        List<SysOperationRecord> exportList = operationRecordService.pageExportList(operationRecord, queryWrapper,new LinkedList<>());
        for (SysOperationRecord record : exportList) {
            record.setStatusStr(record.getStatusStr());
        }

        Field[] fields = SysOperationRecord.class.getDeclaredFields();
        String url = exportService.pageExport(exportList,fields,exportService.listStrByModuleAndUser
                ("sys_operation_record",SecurityUtils.getUserId()), "操作日志信息");
        return JsonResult.success(url);
    }

    /**
     * 获取操作日志详情
     * @param id 操作日志id
     * @return
     */
    @ApiOperation(value = "获取操作日志详情")
    @GetMapping("/getOperationRecordDetail/{id}")
    public JsonResult getOperationRecordDetail(@PathVariable(value = "id") Long id) {
        SysOperationRecord operationRecord = operationRecordService.getById(id);
        return JsonResult.success(operationRecord);
    }

}
